# -*- coding: UTF-8 -*-
from pygame.locals import *
from utils import *
from tile import BurningTile
from movable import Movable

import pygame

class Guy(pygame.sprite.Sprite, Movable):
    events = [K_UP, K_DOWN, K_LEFT, K_RIGHT, K_SPACE]
    game = None
    
    def __init__(self, pos, game):
        pygame.sprite.Sprite.__init__(self)
        # Magical numbers FTW :)
        # Chicks dig magic.
        Movable.__init__(self, pos, (0, 0), 0.023)
        Guy.game = game
        self.tick = 0

        self.image = pygame.transform.scale2x(pygame.image.load("bcup_stand.png")).convert()
        self.rect = self.image.get_rect()

        self.MAXENERGY = 100
        BurningTile.precalculateEnergyLevelColors()
        self.fires = pygame.sprite.Group()
        self.reserved_fires = pygame.sprite.Group()
        self.active_fires = pygame.sprite.Group()
        self.image.set_colorkey((255,0,255)) # Bright Pink

    def get_used_energy(self):
        return len(self.fires) + len(self.reserved_fires) / 1.5
    
    def has_energy(self):
        return self.get_used_energy() < self.MAXENERGY

    def burn(self, time_passed):
        if self.has_energy():
            self.fires.add(BurningTile((self.x,self.y)))
        for fire in self.fires:
            fire.move(time_passed)
#        for fire in self.reserved_fires:
#            fire.move(time_passed)
        for fire in self.active_fires:
            fire.move(time_passed)

    def draw(self, camera):
        rect = self.image.get_rect()
        rect.topleft = (self.x - self.rect.w / 2, self.y - self.rect.h / 2)
        for fire in self.fires:
            fire.draw(camera)
        camera.blit(self.image, rect)
        for fire in self.reserved_fires:
            fire.draw(camera)
        for fire in self.active_fires:
            fire.draw(camera)

    def move(self, time_passed):
        Movable.move(self, time_passed)
        self.burn(time_passed)
        size = self.game.size
        self.x = Clamp(self.x, self.rect.w/2, size[0] - self.rect.w/2)
        self.y = Clamp(self.y, self.rect.h/2, size[1] - self.rect.h/2)

    def handle_input(self):
        x,y = self.direction
        delta = 0.05

        mods = self.game.key_mods
        keys = self.game.keys

        if (mods & (KMOD_LCTRL | KMOD_RCTRL)):
            will_fire = False
            if self.has_energy():
                will_fire = True
            elif len(self.fires):
                will_fire = True
                for fire in self.fires:
                    fire.kill()
                    break
            if will_fire:
                self.reserved_fires.add(BurningTile((self.x,self.y), energy = 300))

        if (mods & (KMOD_LSHIFT | KMOD_RSHIFT)):
            shot_speed = 0.75
            dir_x = 0
            dir_y = 0
            if keys[K_UP]:
                dir_y -= shot_speed
            if keys[K_DOWN]:
                dir_y += shot_speed
            if keys[K_LEFT]:
                dir_x -= shot_speed
            if keys[K_RIGHT]:
                dir_x += shot_speed
            if dir_x != 0 and dir_y != 0:
                # So that the stream will be equally long in all directions
                # (Disregarding any other effects, that is - gravity, for one)
                dir_x /= 2**0.25
                dir_y /= 2**0.25
            
            if dir_x != 0 or dir_y != 0:
                for fire in self.reserved_fires:
                    fire.direction = (dir_x, dir_y)
                    fire.energy = intRandom(100) + 200 #300
                    self.active_fires.add(fire)
                self.reserved_fires.empty()
            if self.has_energy():
                self.fires.add(BurningTile((self.x,self.y), (dir_x,dir_y), 0.023))
        else:
            if keys[K_UP]:
                y -= delta
            if keys[K_DOWN]:
                y += delta
            if keys[K_LEFT]:
                x -= delta
            if keys[K_RIGHT]:
                x += delta
            if keys[K_SPACE]:
                x,y = 0, 0
            x = Clamp(x,-0.66,0.66)
            y = Clamp(y,-0.66,0.66)
            self.direction = (x,y)
